<template>
  <div class="goods-comment">
    <div class="head" v-if="commentInfo">
      <div class="data">
        <p>
          <span>{{ commentInfo.salesCount }}</span
          ><span>人购买</span>
        </p>
        <p>
          <span>{{ commentInfo.praisePercent }}</span
          ><span>好评率</span>
        </p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd">
          <a
            v-for="item in commentInfo.tags"
            :key="item.title"
            href="javascript:;"
            @click="changeTag(item.title)"
            :class="{ active: activeTitle === item.title }"
          >
            {{ item.title }}（{{ item.tagCount }}）
          </a>
        </div>
      </div>
    </div>
    <!-- 排序区 -->
    <div class="sort">
      <span>排序：</span>
       <a @click="reqParams.page = 1;reqParams.sortField = null"  :class="{ active: reqParams.sortField === null }" href="javascript:;">默认</a>
      <a @click="reqParams.page = 1;reqParams.sortField = 'createTime'"  :class="{ active: reqParams.sortField === 'createTime' }" href="javascript:;">最新</a>
      <a @click="reqParams.page = 1;reqParams.sortField = 'praiseCount'"  :class="{ active: reqParams.sortField === 'praiseCount' }" href="javascript:;">最热</a>
    </div>
    <!-- 评论列表 -->
    <div class="list">
      <div class="item" v-for="item in list" :key="item.id">
        <div class="user">
          <img :src="item.member.avatar" alt="" />
          <span>{{ $filters.formatNickName(item.member.nickname) }}</span>
        </div>
        <div class="body">
          <div class="score">
            <i class="iconfont icon-wjx01" v-for="i in item.score" :key="i"></i>
            <i
              class="iconfont icon-wjx02"
              v-for="i in 5 - item.score"
              :key="i"
            ></i>
            <span class="attr">
              {{
                $filters.formatSpecs(item.orderInfo.specs)
              }}
            </span>
            <span class="attr">颜色：黑色 尺码：M</span>
          </div>
          <div class="text">{{ item.content }}</div>
          <!-- 图片 -->
          <goods-comment-image v-if="item.pictures.length" :pictures="item.pictures" ></goods-comment-image>
          <div class="time">
            <span>{{ item.createTime }}</span>
            <span class="zan"
              ><i class="iconfont icon-dianzan"></i>{{ item.praiseCount }}</span
            >
          </div>
        </div>
      </div>
    </div>
    <!-- 分页组件 -->
    <xtx-pagination :total="total" v-model:current-page="reqParams.page" ></xtx-pagination>
  </div>
</template>

<script>
/*
  分页器组件内需要准备数据：
    总条数 props
    当前页码
    按钮个数
    页容量
    分页信息（动态计算）
      总页数
      起始按钮
      结束按钮
      按钮数组（生成哪些编号）
*/
import { inject, reactive, ref, watch } from 'vue'
import { findCommentInfoByGoods, findCommentListByGoods } from '@/api/goods'
import goodsCommentImage from './goods-comment-image.vue'
import XtxPagination from '@/components/library/xtx-pagination.vue'

export default {
  components: { goodsCommentImage, XtxPagination },
  name: 'GoodsComment',
  setup () {
    const goods = inject('goods')
    // 获取评价分页数据
    const list = ref([])
    const total = ref(0)
    // const currentPage = ref(1)
    // 分页接口参数
    const reqParams = reactive({
      page: 1,
      pageSize: 10,
      tag: null,
      hasPicture: false,
      sortField: null,
      sortMethod: null
    })
    watch(reqParams, () => {
      findCommentListByGoods(goods.value.id, { page: 1 }).then(({ result }) => {
      // console.log(2222222, result)
      // item才是列表数据
        total.value = result.counts
        list.value = result.items
      })
    }, { immediate: true })

    // 头部评价信息
    const commentInfo = ref(null)
    // 获取商品的评价信息
    findCommentInfoByGoods(goods.value.id).then(({ result }) => {
      // console.log(result)
      // console.log(111111, result)
      // 处理 tags 数组，方便模板 v-for 遍历
      result.tags = [
        { title: '全部评价', tagCount: result.evaluateCount },
        { title: '有图', tagCount: result.hasPictureCount },
        // 原始的 tags
        ...result.tags
      ]
      commentInfo.value = result
    })
    // 高亮的标签标题
    const activeTitle = ref('全部评价')
    // 筛选条件
    const changeTag = (tag) => {
      // 实现高亮效果的
      activeTitle.value = tag
      // 收集请求参数的三种情况
      if (tag === '全部评价') {
        reqParams.tag = null
        reqParams.hasPicture = false
      } else if (tag === '有图') {
        reqParams.tag = null
        reqParams.hasPicture = true
      } else {
        reqParams.tag = tag
        reqParams.hasPicture = false
      }
      // 筛选参数变化，页码要重置为 1
      reqParams.page = 1
    }

    return { commentInfo, activeTitle, list, changeTag, reqParams, total }
  }
}
</script>

<style scoped lang="less">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      width: 340px;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;
      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }
      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        > a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;
          &:hover {
            border-color: @xtxColor;
            background: lighten(@xtxColor, 50%);
            color: @xtxColor;
          }
          &.active {
            border-color: @xtxColor;
            background: @xtxColor;
            color: #fff;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }
  .list {
    padding: 0 20px;
    .item {
      display: flex;
      padding: 25px 10px;
      border-bottom: 1px solid #f5f5f5;
      .user {
        width: 160px;
        img {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          overflow: hidden;
        }
        span {
          padding-left: 10px;
          color: #666;
        }
      }
      .body {
        flex: 1;
        .score {
          line-height: 40px;
          .iconfont {
            color: #ff9240;
            padding-right: 3px;
          }
          .attr {
            padding-left: 10px;
            color: #666;
          }
        }
      }
      .text {
        color: #666;
        line-height: 24px;
      }
      .time {
        color: #999;
        display: flex;
        justify-content: space-between;
        margin-top: 5px;
      }
    }
  }
}
</style>
